package org.mch.main.panel;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import org.apache.commons.lang3.StringUtils;
import org.mch.biz.ui.BizEvent;
import org.mch.biz.ui.BizPanel;
import org.mch.comm.exec.MsgException;
import org.mch.func.entity.MFuncEO;
import org.mch.main.MCHFrame;
import org.mch.start.MCHCache;

public class MainPanel extends BizPanel {
	private static final long serialVersionUID = -7897328121945334625L;
	private JSplitPane splitPane;
	private JPanel panelLeft;
	private JPanel panelRight;
	private JScrollPane scrollPane;
	private JTree tree;

	public MainPanel() {
		super();
		initGUI();
	}

	private void initGUI() {
		setLayout(new BorderLayout(0, 0));
		add(getSplitPane(), BorderLayout.CENTER);
	}

	public JSplitPane getSplitPane() {
		if (splitPane == null) {
			splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, getPanelLeft(), getPanelRight());
			splitPane.setDividerSize(8);
			// splitPane.setDividerSize(3);
			// splitPane.setLeftComponent(getPanelLeft());
			// splitPane.setRightComponent(getPanelRight());
			splitPane.setOneTouchExpandable(true);
			// splitPane.setDividerLocation(200);
			// splitPane.setDividerLocation(0.1);
			// splitPane.setResizeWeight(0.3);
			splitPane.setDividerLocation(200);

			splitPane.updateUI();
		}
		return splitPane;
	}

	public JPanel getPanelLeft() {
		if (panelLeft == null) {
			panelLeft = new JPanel();
			panelLeft.setLayout(new BorderLayout(0, 0));
			panelLeft.add(getScrollPane(), BorderLayout.CENTER);
		}
		return panelLeft;
	}

	public JPanel getPanelRight() {
		if (panelRight == null) {
			panelRight = new JPanel();
			panelRight.setLayout(new CardLayout(0, 0));
		}
		return panelRight;
	}

	public JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTree());
		}
		return scrollPane;
	}

	private FunTreeClickEvent treeEvent;

	public FunTreeClickEvent getTreeEvent() {
		if (treeEvent == null) {
			treeEvent = new FunTreeClickEvent();
		}
		return treeEvent;
	}

	public JTree getTree() {
		if (tree == null) {
			tree = new JTree();
			tree.setFont(new Font("宋体", Font.PLAIN, 13));
			tree.setModel(new MFuncTreeModel().getTreeModel());
			tree.addMouseListener(getTreeEvent());
		}
		return tree;
	}

	@Override
	protected BizEvent createEvent() {
		return new MainPanelEvent(this);
	}

	public class FunTreeClickEvent extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			if (e.getClickCount() == 2) {
				try {
					onClickTree(e);
				} catch (MsgException e1) {
					MCHFrame.setMsg(e1.getMessage());
				} catch (Exception e1) {
					log().error(e1);
				}
			}
		}
	}

	/**
	 * 双击树事件方法；
	 * 
	 * @param e
	 * @throws Exception
	 */
	protected void onClickTree(MouseEvent e) throws Exception {
		JTree tree = (JTree) e.getSource();
		int selrow = tree.getRowForLocation(e.getX(), e.getY());
		TreePath selpath = tree.getPathForLocation(e.getX(), e.getY());
		if (selrow == -1) {
			return;
		}

		/*
		 * 如果双击的是树功能节点，则构建显示对应功能Panel；
		 */
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) selpath.getLastPathComponent();
		MFuncEO nodeEO = (MFuncEO) node.getUserObject();

		final String funcClassName = nodeEO.getClassname();
		if (!StringUtils.isEmpty(funcClassName)) {
			openFuncPanel(funcClassName);
		}
	}

	protected void openFuncPanel(String funcClassName) {
		JPanel mainPanel = getPanelRight();
		CardLayout layout = (CardLayout) mainPanel.getLayout();
		BizPanel funcPanel = MCHCache.getFuncPanel(funcClassName);
		if (funcPanel == null) {
			try {
				funcPanel = (BizPanel) Class.forName(funcClassName).newInstance();
				mainPanel.add(funcPanel, funcClassName);

				layout.next(mainPanel);

				MCHCache.setFuncPanel(funcPanel);
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				throw new MsgException("当前功能不在此环境中！");
			}
		} else {
			layout.show(mainPanel, funcClassName);
		}

		log().debug(funcClassName);
	}
}
